package com.blw.demoactiviti.config.activiti;

import com.blw.demoactiviti.testbean.UserBean;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.activiti.engine.impl.history.HistoryLevel;
import org.activiti.engine.impl.interceptor.CommandInterceptor;
import org.activiti.spring.ProcessEngineFactoryBean;
import org.activiti.spring.SpringProcessEngineConfiguration;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;

/**
 * @author: baolw
 * @date 2017/12/6 10:56
 */
@Configuration
//public class ActivitiConfig  extends AbstractProcessEngineAutoConfiguration {
public class ActivitiConfig{

    /*@Bean
    public SpringProcessEngineConfiguration springProcessEngineConfiguration(@Qualifier("dataSource") DataSource dataSource
            , PlatformTransactionManager transactionManager, SpringAsyncExecutor springAsyncExecutor) throws IOException {
        return this.baseSpringProcessEngineConfiguration(dataSource, transactionManager, springAsyncExecutor);
    }*/

    @Bean
    public ProcessEngineConfigurationImpl springProcessEngineConfiguration(@Qualifier("dataSource") DataSource dataSource
            ,PlatformTransactionManager transactionManager){
//        StandaloneProcessEngineConfiguration configuration = new SpringProcessEngineConfiguration();
        SpringProcessEngineConfiguration configuration = new SpringProcessEngineConfiguration();
        configuration.setDataSource(dataSource);
        configuration.setActivityFontName("宋体");
        configuration.setLabelFontName("宋体");
        configuration.setXmlEncoding("UTF-8");
        configuration.setHistory(HistoryLevel.AUDIT.getKey());
        configuration.setTransactionManager(transactionManager);
        configuration.setDeploymentMode("single-resource");
//        configuration.setCreateDiagramOnDeploy(true);在部署的时候，并不需要或者不必要生成流程定义图片
        return configuration;
    }

    @Bean
    public ProcessEngine processEngineFactoryBean(ProcessEngineConfigurationImpl configuration) throws Exception {
        ProcessEngineFactoryBean bean = new ProcessEngineFactoryBean();
        bean.setProcessEngineConfiguration(configuration);
        return bean.getObject();
    }

    @Bean
    public UserBean getUserBean(ProcessEngine processEngine) {
        UserBean userBean = new UserBean();
        userBean.setRuntimeService(processEngine.getRuntimeService());
        return userBean;
    }

}
